package cool.taomu.software.fig.spring.example;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cool.taomu.software.fig.mqtt.client.paho.annotation.MqttClient;
import cool.taomu.software.fig.mqtt.client.paho.annotation.Subscriber;
import cool.taomu.software.fig.mqtt.client.paho.entity.SenderEntity;
import cool.taomu.software.fig.spring.example.service.Collection;

@MqttClient()
public class Example {
	private final static Logger LOG = LoggerFactory.getLogger(Example.class);

	 @Autowired
	 Collection collection;

	SenderEntity sender = new SenderEntity("get/code");

	@Subscriber(topic = "start")
	public void start(String topic, MqttMessage message) {
		collection.p("Hello World");
		/*Requests req = new Requests();
		String html = req.get("http://www.txsec.com/inc1/gpdm.asp").execute().body();
		return sender.clone().message(html).topic("get/code");
		*/
	}

	@Subscriber(topic = "set/result")
	public List<SenderEntity> results(String topic, MqttMessage message) {
		String codes = new String(message.getPayload());
		List<SenderEntity> sends = new ArrayList<>();
		for (String code : codes.split(",")) {
			sends.add(sender.clone().message(code).topic("start/collection"));
		}
		return sends;
	}

	@Subscriber(topic = "start/collection")
	public void coll(String topic, MqttMessage message) {
		String code = new String(message.getPayload());
		collection.collection(code);
	}

}
